package com.wanxx.controller;

import com.wanxx.service.StudentService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.wanxx.domain.Student;

import javax.annotation.Resource;

@Controller
public class RequestController {

    @Resource
    private StudentService service;

    //注册学生
    @RequestMapping("/forward.do")
    public ModelAndView addStudentforward(Student student) {
        ModelAndView mv = new ModelAndView();
        mv.addObject("msg", "request");
        int num = service.addStudent(student);
        String tips = "注册失败";
        if (num > 0 && student.getName() != null) {
            mv.addObject("tips", "注册【" + student.getName() + "】成功");
        }
        //指定结果页面
        mv.setViewName("forward:/WEB-INF/jsp/request.jsp");
        return mv;
    }

    @RequestMapping("/redirect.do")
    public ModelAndView addStudent(Student student) {
        ModelAndView mv = new ModelAndView();
        int num = service.addStudent(student);
        mv.addObject("msg", "redirect");
        String tips = "注册失败";
        if (num > 0 && student.getName() != null) {
            mv.addObject("tips", "注册【" + student.getName() + "】成功");
        }
        mv.addObject("name", student.getName());
        mv.addObject("age", student.getAge());

        //指定结果页面
        //重定向不能访问WEB-INF文件夹
        //mv.setViewName("forward:/WEB-INF/jsp/request.jsp");
        mv.setViewName("redirect:/request.jsp");
        return mv;
    }
}
